﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;
using UMC.Data;
using UMC.WorkFlow.Entities;

namespace UMC.WorkFlow.Subassembly
{
    [UIDesign(Name = "通知消息", Explain = "通知消息")]
    public class NotifylMessage : UMC.WorkFlow.Responder
    {
        string sonsorShapeId = "ALL";
        [Design(Caption = "触发流程结点", ArgumentType = ArgumentType.Shape)]
        public string ShapeId
        {
            get
            {
                return sonsorShapeId;
            }
            set
            {
                sonsorShapeId = value;
            }
        }
        protected override void OnRespond(UMC.WorkFlow.InstanceEvent taskEvent)
        {
            if (this.TextSetting != null && this.IdentitySetting != null)
            {
                taskEvent.OnEndProcess += OnEndProcess;
            }
        }

        [Design(Caption = "设置接收人", Explain = "设置接收人")]
        public IdentitySetting IdentitySetting { get; set; }
        void OnEndProcess(UMC.WorkFlow.TaskInstance task, UMC.WorkFlow.InstanceEventArgs e)
        {
            if (String.IsNullOrEmpty(sonsorShapeId) == false && String.Equals(sonsorShapeId, "ALL", StringComparison.CurrentCultureIgnoreCase) == false)
            {
                if (e.Shape.Id.StartsWith(sonsorShapeId) == false)
                {
                    return;
                }
                if (task.Shapes.Exists(s => s.Id.StartsWith(sonsorShapeId) && s.Status == ShapeStatus.Activation))
                {
                    return;
                }
            }


            var es = Utility.Enum(this.TextSetting.CommandType);


            foreach (var ct in es)
            {
                if (ct == e.CommandType)
                {
                    if (!string.IsNullOrEmpty(TextSetting.Text) && this.IdentitySetting != null)
                    {
                        System.Collections.IDictionary dic = new System.Collections.Hashtable(e.Runtime.Items);
                        var SessionId = (ulong)dic[TaskRuntime.EVENT_SESSION_ID];

                        var subject = Utility.Format("{UI.Subject}", dic);

                        var table = SubassemblyHelper.Identity(this.IdentitySetting, dic, task, e.Shape, this.Id);
                        if (table != null)
                        {

                            var taskMessages = new List<TaskMessage>();

                            for (var i = 0; i < table.Length; i++)
                            {

                                taskMessages.Add(new TaskMessage()
                                {
                                    Id = Utility.NewId(),
                                    Subject = subject,
                                    Type = 3,
                                    CreationTime = DateTime.Now,
                                    ShapeId = e.Shape.Id,
                                    Status = 0,
                                    InstanceId = task.InstanceId,
                                    Body = Utility.Format(this.TextSetting.Text, dic),
                                    Sender = e.Identity.Username,
                                    SenderAlias = e.Identity.Alias,
                                    Proposer = task.Identity.Username,
                                    ProposerAlias = task.Identity.Alias,
                                    TaskId = task.TaskId,
                                    SessionId = SessionId,
                                    Username = table[i]
                                });

                            }
                            if (taskMessages.Count > 0)
                            {
                                DataFactory.Instance().Put(taskMessages);
                                // TaskRuntime.Database.ObjectEntity<TaskMessage>().Insert(taskMessages.ToArray());
                            }
                        }

                    }
                }

            }

        }


        [WorkFlow.Design(Caption = "消息内容格式")]
        public TextSetting TextSetting { get; set; }


        protected override void WriteProperty(System.IO.TextWriter writer)
        {
            writer.Write(',');
            JSON.Serialize("ShapeId", this.ShapeId, writer);

            if (this.IdentitySetting != null)
            {
                writer.Write(',');
                JSON.Serialize("IdentitySetting", this.IdentitySetting, writer);
            }
            if (this.TextSetting != null)
            {
                writer.Write(',');
                JSON.Serialize("TextSetting", this.TextSetting, writer);
            }


            base.WriteProperty(writer);
        }
        public override Func<object> GetInstance(string prototypeName)
        {
            switch (prototypeName)
            {
                case "IdentitySetting":
                    return () => new IdentitySetting();
                case "TextSetting":
                    return () => new TextSetting();
                default:
                    return base.GetInstance(prototypeName);

            }
        }
        public override void Read(string key, object value)
        {
            switch (key)
            {
                case "ShapeId":
                    this.ShapeId = value as string;
                    break;
                case "IdentitySetting":
                    this.IdentitySetting = value as IdentitySetting;
                    break;
                case "TextSetting":
                    this.TextSetting = value as TextSetting;
                    break;

            }
            base.Read(key, value);
        }


    }
}
